<script setup lang="ts">
import { computed, ref, nextTick } from 'vue';
import { usePermissionStoreWithOut } from '@/store/modules/permission';
import Fuse from 'fuse.js';
import path from 'path-browserify';
import { Search } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
const permissionStore = usePermissionStoreWithOut();
const search = ref('');
const show = ref(false);
const fuse = ref();
const searchPool = ref<any>([]);
const options: any = ref([]);
const router = useRouter();

const routes = computed(() => {
  return permissionStore.getRouters;
});
const headerSearchSelect = ref();

function click() {
  show.value = !show.value;
  if (show.value) {
    headerSearchSelect.value?.focus();
  }
}
function close() {
  headerSearchSelect.value?.blur();
  options.value = [];
  show.value = false;
}
function change(val) {
  console.log(val);
  router.push(val.path);
  search.value = '';
  options.value = [];
  nextTick(() => {
    show.value = false;
  });
}

function initFuse(list) {
  console.log('???');
  console.log(list);
  fuse.value = new Fuse(list, {
    shouldSort: true,
    threshold: 0.4,
    location: 0,
    distance: 100,

    minMatchCharLength: 1,
    keys: [
      {
        name: 'title',
        weight: 0.7,
      },
      {
        name: 'path',
        weight: 0.3,
      },
    ],
  });
}

function generateRoutes(routes: any, basePath = '/', prefixTitle = []): any {
  let res: any = [];

  for (const router of routes) {
    // skip hidden router
    if (router.hidden) {
      continue;
    }

    const data = {
      path: path.resolve(basePath, router.path),
      title: [...prefixTitle],
    };

    if (router.meta && router.meta.title) {
      data.title = [...data.title, router.meta.title];

      if (router.redirect !== 'noRedirect') {
        res.push(data);
      }
    }

    // recursive child routes
    if (router.children) {
      const tempRoutes = generateRoutes(router.children, data.path, data.title);
      if (tempRoutes.length >= 1) {
        res = [...res, ...tempRoutes];
      }
    }
  }

  return res;
}
function querySearch(query) {
  if (query !== '') {
    options.value = fuse.value.search(query);
    console.log('搜索');
    console.log(fuse.value.search(query));
  } else {
    options.value = [];
  }
}
searchPool.value = generateRoutes(routes.value);

initFuse(searchPool.value);
</script>

<template>
  <div :class="{ show: show }" class="header-search">
    <el-select
      ref="headerSearchSelect"
      v-model="search"
      :remote-method="querySearch"
      filterable
      default-first-option
      remote
      placeholder="搜索菜单"
      class="header-search-select"
      @change="change"
    >
      <el-option
        v-for="item in options"
        :key="item.item.path"
        :value="item.item"
        :label="item.item && item.item.title.join(' > ')"
      />
    </el-select>
    <el-icon class="search-icon" @click.stop="click"><Search /></el-icon>
  </div>
</template>

<style lang="scss" scoped>
.header-search {
  font-size: 0 !important;

  .search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    ::v-deep .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>
